# very valuable core code


inputFileName = "/data4/team/weijiang/human_judge_web_pages_plain_text_only_words/enwp01/clueweb09-enwp01-31-11362_76882_plain_text.txt"
inputFileHandler = open(inputFileName,"r")
doc_content = inputFileHandler.read()
doc_words = doc_content.strip().split(" ")


M_Constant_32 = 32
M_Constant_8 = 8
M_Constant_4 = 4
M_Constant_1 = 1

N_Constant_32 = 32
N_Constant_8 = 8
N_Constant_4 = 4
N_Constant_1 = 1

df_M_1 = 0
df_M_4 = 0
df_M_8 = 0
df_M_32 = 0

df_wN_1 = 0
df_wN_4 = 0
df_wN_8 = 0
df_wN_32 = 0

tf_M_1 = 0
tf_M_4 = 0
tf_M_8 = 0
tf_M_32 = 0

tf_wN_1 = 0
tf_wN_4 = 0
tf_wN_8 = 0
tf_wN_32 = 0

mode = "ordered" # "unordered"

# print doc_words
indices1 = [i for i, x in enumerate(doc_words) if x == "obama"]
indices2 = [i for i, x in enumerate(doc_words) if x == "family"]
indices3 = [i for i, x in enumerate(doc_words) if x == "tree"]

indicesList = []
indicesList.append(indices1)
indicesList.append(indices2)
indicesList.append(indices3)

print "raw indices"
print indices1
print indices2
print indices3

if mode == "ordered":
    # Let's do the M computation
    # need to assign the head indices
    # a dict structure for final judge
    tempImportantPositionsList_M32 = []
    tempImportantPositionsList_M8 = []
    tempImportantPositionsList_M4 = []
    tempImportantPositionsList_M1 = []
    
    indicesConditionDict_M32 = {}
    indicesConditionDict_M8 = {}
    indicesConditionDict_M4 = {}
    indicesConditionDict_M1 = {}
    
    for i in range(0,len(indicesList)):
        indicesConditionDict_M32[i] = False
        indicesConditionDict_M8[i] = False
        indicesConditionDict_M4[i] = False
        indicesConditionDict_M1[i] = False
    # print "old indicesConditionDict_M32:",indicesConditionDict_M32
    # set the head indices to be True
    indicesConditionDict_M32[0] = True
    indicesConditionDict_M8[0] = True
    indicesConditionDict_M4[0] = True
    indicesConditionDict_M1[0] = True
    
    # assignment loop
    for indexNumberFromIndex1 in indicesList[0]:
        
        tempImportantPositionsList_M32.append( (indexNumberFromIndex1,0) )
        tempImportantPositionsList_M8.append( (indexNumberFromIndex1,0) )
        tempImportantPositionsList_M4.append( (indexNumberFromIndex1,0) )
        tempImportantPositionsList_M1.append( (indexNumberFromIndex1,0) )
        
        for index,currentIndices in enumerate(indicesList[1:]):
            # for the other indices
            for currentIndicesPositionNumber in currentIndices:
                # core judge statements
                if currentIndicesPositionNumber >= indexNumberFromIndex1 and currentIndicesPositionNumber <= indexNumberFromIndex1 + M_Constant_1:
                    indicesConditionDict_M1[index+1] = True
                    tempImportantPositionsList_M1.append( (currentIndicesPositionNumber,index+1) )
                
                if currentIndicesPositionNumber >= indexNumberFromIndex1 and currentIndicesPositionNumber <= indexNumberFromIndex1 + M_Constant_4:
                    indicesConditionDict_M4[index+1] = True
                    tempImportantPositionsList_M4.append( (currentIndicesPositionNumber,index+1) )
                
                if currentIndicesPositionNumber >= indexNumberFromIndex1 and currentIndicesPositionNumber <= indexNumberFromIndex1 + M_Constant_8:
                    indicesConditionDict_M8[index+1] = True
                    tempImportantPositionsList_M8.append( (currentIndicesPositionNumber,index+1) )
                
                if currentIndicesPositionNumber >= indexNumberFromIndex1 and currentIndicesPositionNumber <= indexNumberFromIndex1 + M_Constant_32:
                    indicesConditionDict_M32[index+1] = True
                    tempImportantPositionsList_M32.append( (currentIndicesPositionNumber,index+1) )
                    break
              
        # judge loop
        allConditionSafisfiedFlag_M32 = True
        allConditionSafisfiedFlag_M8 = True
        allConditionSafisfiedFlag_M4 = True
        allConditionSafisfiedFlag_M1 = True
        

        for i in indicesConditionDict_M32:
            if indicesConditionDict_M32[i] == False:
                allConditionSafisfiedFlag_M32 =False

        for i in indicesConditionDict_M8:
            if indicesConditionDict_M8[i] == False:
                allConditionSafisfiedFlag_M8 =False

        for i in indicesConditionDict_M4:
            if indicesConditionDict_M4[i] == False:
                allConditionSafisfiedFlag_M4 =False

        for i in indicesConditionDict_M1:
            if indicesConditionDict_M1[i] == False:
                allConditionSafisfiedFlag_M1 =False


        if allConditionSafisfiedFlag_M32:
            print 
            #print indicesConditionDict_M32
            #print tempImportantPositionsList_M32
            
            minPosition = 999999
            maxPosition = 0
            outputLine = ""
            for currentTuple in tempImportantPositionsList_M32:
                (currentPosition,_) = currentTuple
                
                outputLine += doc_words[currentPosition] + " "
                
                if currentPosition < minPosition:
                    minPosition = currentPosition
                
                if currentPosition > maxPosition:
                    maxPosition = currentPosition
            #print "minPosition:",minPosition
            #print "maxPosition:",maxPosition
            #print outputLine
            #print doc_words[minPosition:maxPosition+1]
            tf_M_32 += 1

        if allConditionSafisfiedFlag_M8:
            print 
            print indicesConditionDict_M8
            print tempImportantPositionsList_M8
            
            minPosition = 999999
            maxPosition = 0
            outputLine = ""
            for currentTuple in tempImportantPositionsList_M8:
                (currentPosition,_) = currentTuple
                
                outputLine += doc_words[currentPosition] + " "
                
                if currentPosition < minPosition:
                    minPosition = currentPosition
                
                if currentPosition > maxPosition:
                    maxPosition = currentPosition
            print "minPosition:",minPosition
            print "maxPosition:",maxPosition
            print outputLine
            print doc_words[minPosition:maxPosition+1]
            tf_M_8 += 1

        if allConditionSafisfiedFlag_M4:
            print 
            #print indicesConditionDict_M4
            #print tempImportantPositionsList_M4
            
            minPosition = 999999
            maxPosition = 0
            outputLine = ""
            for currentTuple in tempImportantPositionsList_M4:
                (currentPosition,_) = currentTuple
                
                outputLine += doc_words[currentPosition] + " "
                
                if currentPosition < minPosition:
                    minPosition = currentPosition
                
                if currentPosition > maxPosition:
                    maxPosition = currentPosition
            #print "minPosition:",minPosition
            #print "maxPosition:",maxPosition
            #print outputLine
            #print doc_words[minPosition:maxPosition+1]
            tf_M_4 += 1
        
        # for M1
        if allConditionSafisfiedFlag_M1:
            print 
            print indicesConditionDict_M1
            print tempImportantPositionsList_M1
            
            minPosition = 999999
            maxPosition = 0
            outputLine = ""
            for currentTuple in tempImportantPositionsList_M1:
                (currentPosition,_) = currentTuple
                
                outputLine += doc_words[currentPosition] + " "
                
                if currentPosition < minPosition:
                    minPosition = currentPosition
                
                if currentPosition > maxPosition:
                    maxPosition = currentPosition
            print "minPosition:",minPosition
            print "maxPosition:",maxPosition
            print outputLine
            print doc_words[minPosition:maxPosition+1]
            tf_M_1 += 1            
            
                     
        # reset the judge dict
        for i in range(0,len(indicesList)):
            indicesConditionDict_M32[i] = False
        indicesConditionDict_M32[0] = True
        del tempImportantPositionsList_M32[:]
        
        # reset the judge dict
        for i in range(0,len(indicesList)):
            indicesConditionDict_M8[i] = False
        indicesConditionDict_M8[0] = True
        del tempImportantPositionsList_M8[:]
        
        # reset the judge dict
        for i in range(0,len(indicesList)):
            indicesConditionDict_M4[i] = False
        indicesConditionDict_M4[0] = True
        del tempImportantPositionsList_M4[:]
        
        # reset the judge dict
        for i in range(0,len(indicesList)):
            indicesConditionDict_M1[i] = False
        indicesConditionDict_M1[0] = True
        del tempImportantPositionsList_M1[:]
    
    if tf_M_32 != 0:
        df_M_32 = 1
        
    if tf_M_8 != 0:
        df_M_8 = 1
        
    if tf_M_4 != 0:
        df_M_4 = 1
        
    if tf_M_1 != 0:
        df_M_1 = 1  
          
    print "tf_M_32:",tf_M_32
    print "tf_M_8:",tf_M_8
    print "tf_M_4:",tf_M_4
    print "tf_M_1:",tf_M_1
    
    print "df_M_32:",df_M_32
    print "df_M_8:",df_M_8
    print "df_M_4:",df_M_4
    print "df_M_1:",df_M_1

# Let's do the N second
